package com.qingmeng.applyjob.portal.controller;

import com.qingmeng.applyjob.common.api.CommonResult;
import com.qingmeng.applyjob.portal.enums.RegisterResult;
import com.qingmeng.applyjob.portal.model.dto.UserRegisterParam;
import com.qingmeng.applyjob.portal.model.vo.UserVo;
import com.qingmeng.applyjob.portal.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "UserController", description = "用户接口")
@RequestMapping("/user")
@RestController
public class UserController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @GetMapping("/jwt")
    public CommonResult<String> testSecurity() {
        return CommonResult.success("访问 URL 成功");
    }

    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public CommonResult<Integer> register(@RequestBody UserRegisterParam param) {
        RegisterResult result = userService.register(param);
        if (result != RegisterResult.SUCCESS) {
            CommonResult.failed(result.getMessage());
        }
        return CommonResult.success(1, result.getMessage());
    }

    @ApiOperation("用户登陆")
    @PostMapping("/login")
    public CommonResult<Map<String, String>> login(String username, String password) {
        logger.debug("username = " + username + ", password = " + password);
        Map<String, String> map = userService.login(username, password);
        if (CollectionUtils.isEmpty(map)) {
            return CommonResult.validateFailed("用户名或者密码错误");
        }
        return CommonResult.success(map, "用户登陆成功");
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/getPersonal")
    public CommonResult<UserVo> getPersonal(){
        UserVo personal = userService.getPersonal();
        if (personal != null){
            return CommonResult.success(personal,"获取信息成功");
        }
        return CommonResult.failed("未查询到相关信息");
    }

}
